package com.kou.backend.controller.file;

import com.kou.backend.api.file.FilePreviewClient;
import com.kou.backend.entity.FileResourceDO;
import com.kou.backend.enums.FileOpsTypeEnum;
import com.kou.backend.enums.PreviewFileTypeEnum;
import com.kou.backend.enums.StorageTypeEnum;
import com.kou.backend.service.FileStorageService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author: kou
 * @since: 2025/7/9 17:47
 */
@RestController
public class FilePreviewController implements FilePreviewClient {
    @Resource
    private FileStorageService fileStorageService;
    @Value("${file.storage.type}")
    private String storageType;

    @PreAuthorize("hasAnyAuthority('all')")
    @Override
    public void autoPreview(String fileId, HttpServletResponse response) throws IOException {
        //预览header设置：Content-Disposition: inline
        //如果是浏览器不支持预览的文件类型后续要直接将其强制转到下载
        FileResourceDO fileResourceDO = fileStorageService.getFileResourceDO(fileId);
        long fileSize = fileResourceDO.getFileSize();
        if (!PreviewFileTypeEnum.getByType(fileResourceDO.getFileType()).equals(PreviewFileTypeEnum.OTHER)) {
            if (fileSize < 100 * 1024 * 1024 || !StorageTypeEnum.MINIO.getStorageType().equals(storageType)) {
                // 小于100MB，后端输出流
                fileStorageService.fileStreamToResponse(fileId, FileOpsTypeEnum.PREVIEW.getFileOpsType(), response);
            } else {
                // 大文件，生成预签名URL，返回 302 重定向
                String url = fileStorageService.generatePresignedUrl(fileId, 3600);
                response.setStatus(HttpServletResponse.SC_FOUND);
                response.setHeader("Location", url);
            }
        } else {
            fileStorageService.fileStreamToResponse(fileId, FileOpsTypeEnum.DOWNLOAD_FILE.getFileOpsType(), response);
        }
    }

    @PreAuthorize("hasAnyAuthority('all')")
    @Override
    public void preview(String fileId, HttpServletResponse response) throws IOException {
        //预览header设置：Content-Disposition: inline
        //如果是浏览器不支持预览的文件类型后续要直接将其强制转到下载
        FileResourceDO fileResourceDO = fileStorageService.getFileResourceDO(fileId);
        if (!PreviewFileTypeEnum.getByType(fileResourceDO.getFileType()).equals(PreviewFileTypeEnum.OTHER)) {
            fileStorageService.fileStreamToResponse(fileId, FileOpsTypeEnum.PREVIEW.getFileOpsType(), response);
        } else {
            fileStorageService.fileStreamToResponse(fileId, FileOpsTypeEnum.DOWNLOAD_FILE.getFileOpsType(), response);
        }
    }

    @Override
    public void download(String fileId, HttpServletResponse response) throws IOException {
        FileResourceDO fileResourceDO = fileStorageService.getFileResourceDO(fileId);
        long fileSize = fileResourceDO.getFileSize();
        if (fileSize < 100 * 1024 * 1024 || !StorageTypeEnum.MINIO.getStorageType().equals(storageType)) {
            // 小于100MB，后端输出流
            fileStorageService.fileStreamToResponse(fileId, FileOpsTypeEnum.DOWNLOAD_FILE.getFileOpsType(), response);
        } else {
            // 大文件，生成预签名URL，返回 302 重定向
            String url = fileStorageService.generatePresignedUrl(fileId, 3600);
            response.setStatus(HttpServletResponse.SC_FOUND);
            response.setHeader("Location", url);
        }
    }
}
